import * as echarts from '../../ec-canvas/echarts.min';
let chart = null;
var utils = require('../../utils/util.js');
var httpUrl = getApp().globalData.httpUrl;
var indicator = [];// 雷达图五个点的名称
var indValue1 = [];// 雷达图2条线条的其中第一条的值,
var indValue2 = [];// 雷达图2条线条的其中第二条的值,
var amountData=[];
function initChart(canvas, width, height) {
    chart = echarts.init(canvas, null, {
        width: width,
        height: height
    });
    canvas.setChart(chart);
}
Page({
    data: {
        ec: {
            onInit: initChart
        },
        maxlength: 500,
        inputlength: 0,
        curHdIndex: 0, //教练tab切换
        idx: 1,
        curBdIndex: 0, // 评价老师tab切换
        isarray:false,
        ishide:false
    },
    onLoad: function(e) {
        var that = this;
        that.setData({
            userAvatarUrl: wx.getStorageSync("userInfo").avatarUrl
        })
    },
    onShow: function(e) {
        var that = this;
        //获取当前日期
        var format = utils.formatData(new Date());
        var arr=[];
        for (var i = 2018; i <=format.substr(0, 4);i++){
            console.log(i);
            arr.push(i +"上学期");
            arr.push(i + "下学期")
        }
        that.setData({
            array: arr
        })
        if (format.substr(5, 2) > 7) {
            this.setData({
                dataFormat: format.substr(0, 4) + "下学期"
            })
        } else {
            this.setData({
                dataFormat: format.substr(0, 4) + "上学期"
            })
        }
       
        this.setData({
            forstryear: format.substr(0, 4),
            strInfo: that.data.dataFormat
        })
        console.log(that.data.dataFormat);
        that.interestlist();
        that.getNewList();
        that.ondetailData(that.data.idx);
    },
    //日期选择
    bindPickerChange(e) {
        console.log('picker发送选择改变，携带值为', e)
        this.setData({
            index: e.detail.value,
            isarray: true,
        })
        this.getScoreList(this.data.detailData.coach_id, this.data.array[e.detail.value])
    },
    //我的教练列表展示
    interestlist: function(e) {
        var that = this;
        utils.http(httpUrl + "/mobile/coach/all_list_v2", {}, function(res) {
            if (res.state == 1) {
                that.setData({
                    allist: res.datas
                });
            } else {
                utils.message(res.datas);
            }
        })

    },
    //进度条滑动状态
    // slider4change: function(e) {
    //     var that=this;
    //     this.setData({
    //         value: e.detail.value
    //     })
    //     var str = e.detail.value.toString();
    //     if (str == 2005.5) {
    //         that.setData({
    //             strInfo: ""
    //         })
    //     } else if (str.length > 4) {
    //         that.setData({
    //             strInfo: str.substr(0, 4) + "下学期"
    //         })
    //     } else {
    //         that.setData({
    //             strInfo: str.substr(0, 4) + "上学期"
    //         })
    //     }
    //     this.getScoreList(this.data.detailData.coach_id, that.data.strInfo)
    // },
    //新闻 查看更多
    oNNewMoreTap: function(e) {
        wx.navigateTo({
            url: '../newmore/newmore?type=4',
        })
    },
    onReady() {
        var that = this;
        setTimeout(function() {
            // 获取 chart 实例的方式
            console.log(chart);
        }, 2000);
    },
    //输入文本框获取长度
    bindTextAreaBlur: function(e) {
        var e = e.detail.value;
        this.setData({
            inputlength: e.length
        })
    },
    //教练切换
    onthumbTap: function(e) {
        var that=this;
        var id = e.currentTarget.dataset.id;
        var idx = e.currentTarget.dataset.idx;
        var format = utils.formatData(new Date());
        that.setData({
            curHdIndex: id,
            idx: idx,
            curBdIndex: 0,
            isarray: false,
           
        })
        this.ondetailData(idx);
    },

    //训练数据
    ondetailData: function(typeId) {
        var that = this;
        utils.http(httpUrl + "/mobile/coach/detail", {
            type_id: typeId
        }, function(res) {
            if (res.state == 1) {
                that.setData({
                    detailData: res.datas,
                    state: res.state
                });
                // console.log(that.data.detailData.length)

                that.getScoreList(that.data.detailData.coach_id, that.data.strInfo);
                that.getNewList();
            } else {
                // utils.message(res.datas);
                that.setData({
                    state: res.state
                });
            }
        })
    },
    //雷达图数据
    getScoreList: function(typeId, data) {
        var that = this;
        utils.http(httpUrl + "/mobile/coach/get_score", {
            coach_id: typeId,
            date: data
        }, function(res) {
            if (res.state == 1) {
                that.setData({
                    scoreData: res.datas,
                });
                var scoreData1 = res.datas;
                if (scoreData1.type_list.length>0){
                    that.setData({
                        ishide: false
                    })
                    var x_arr = [];
                    for (var i in scoreData1.type_list) {
                        var x_one = {
                            name: scoreData1.type_list[i],
                            max: 10
                        };
                        x_arr.push(x_one);
                    }
                    indicator = x_arr;
                    indValue1 = scoreData1.end_data; // 学期初
                    indValue2 = scoreData1.start_data; // 学期末
                    var end_amount ="学期末 "+ scoreData1.end_amount+"分" // 学期末
                    var start_amount = "学期初 " + scoreData1.start_amount + "分"// 学期初 
                    amountData = [start_amount,end_amount];
                    var option = {
                        tooltip: {
                        },
                        legend: {
                            data: amountData,
                            textStyle: {
                                fontSize: 12
                            },
                            left:20,
                        },


                        radar: {
                            // shape: 'circle',
                            name: {
                                textStyle: {
                                    color: '#000',
                                    borderRadius: 5,
                                    padding: [0, 0],
                                },

                            },
                            radius: "75%",
                            indicator: indicator,
                            center: ['50%', '55%'],
                            startAngle: "40",
                            splitNumber: "10",
                            nameGap:"10"
                            //shape: 'circle',

                        },
                        series: [{
                            // name: '预算 vs 开销（Budget vs spending）',
                            type: 'radar',
                            // areaStyle: {normal: {}},
                            data: [{
                                value: indValue1,
                                name: end_amount,
                                lineStyle: {
                                    color: {
                                        type: 'linear',
                                        x: 0,
                                        y: 0,
                                        x2: 0,
                                        y2: 1,
                                        colorStops: [{
                                            offset: 0,
                                            color: '#ff8734' // 0% 处的颜色
                                        }, {
                                            offset: 1,
                                            color: '#ff8734' // 100% 处的颜色
                                        }],
                                        globalCoord: false // 缺省为 false
                                    }
                                },
                                itemStyle: {
                                    normal: {
                                        color: '#ff8734'
                                    }
                                }
                            },
                            {
                                value: indValue2,
                                name: start_amount,
                                lineStyle: {
                                    color: {
                                        type: 'solid',
                                        x: 0,
                                        y: 0,
                                        x2: 0,
                                        y2: 1,
                                        colorStops: [{
                                            offset: 0,
                                            color: '#7899ff' // 0% 处的颜色
                                        }, {
                                            offset: 1,
                                            color: '#7899ff' // 100% 处的颜色
                                        }],
                                        globalCoord: false // 缺省为 false
                                    }
                                },
                                itemStyle: {
                                    normal: {
                                        color: '#7899ff'
                                    }
                                }
                            }
                            ],

                        }]
                    };
                    chart.setOption(option);
                }else{
                    that.setData({
                        ishide:true
                    })
                }
            
            } else {
                 utils.message(res.datas);
            }
        })
    },

    //兴趣班新闻
    getNewList: function(e) {
        var that = this;
        utils.http(httpUrl + "/mobile/coach/article", {}, function(res) {
           // console.log(res);
            if (res.state == 1) {
                that.setData({
                    newList: res.datas
                })
            } else {
                that.setData({
                    state: res.state
                });
            }
        })
    },

    //评价老师
    tabTap: function(e) {
        var that = this;
        var dataId = e.currentTarget.id;
        this.setData({
            curBdIndex: dataId
        })
        if (that.data.allist.length){
        if (that.data.detailData.my_coach_comment == 0) {
            wx.showModal({
                title: '温馨提示',
                content: '您确定评价吗？',
                success(res) {
                    if (res.confirm) {
                        utils.http(httpUrl + "/mobile/coach/make_coach_score", {
                            coach_id: that.data.detailData.coach_id,
                            score: dataId
                        }, function(res) {
                            if (res.state == 1) {
                                utils.message("评论成功");
                                that.ondetailData(that.data.idx)
                            } else {
                                utils.message(res.datas);

                            }
                        })
                    } else if (res.cancel) {
                        // console.log('用户点击取消')
                    }
                }
            })
        } else {
            utils.message("您以评论过该教练");
        }
        }
    },

    //意见反馈
    formSubmit: function(e) {
        var that = this;
        utils.http(httpUrl + "/mobile/common/feedback", {
            content: e.detail.value.textarea,
            type:1
        }, function(res) {
            if (res.state == 1) {
                wx.showToast({
                    title: '反馈成功',
                })
            } else {
                utils.message(res.datas);
            }
        })
    }


});